#ifndef __MQTT_CLIENT_H__
#define __MQTT_CLIENT_H__


#include <unordered_map>
#include <string>

#include "mosquittopp.h"
#include "config.h"
#include "ubt_utils.h"
#include <ubt_mqtt/node.h> 

namespace ubt {

class CMqttClient : public mosqpp::mosquittopp
{
public:
    typedef std::function<void(const char*, int)> onMessageCallback;

    CMqttClient();
    virtual ~CMqttClient();

    void on_connect(int rc);
    void on_disconnect(int rc);
    void on_message(const struct mosquitto_message *message);
    void on_subscribe(int mid, int qos_count, const int *granted_qos);
    void on_log(int level, const char *str);

    void Connect(const std::string &addr, int port, int keepalive = 60);
    bool Publish(const char *topic, int payloadlen = 0, const void *payload = NULL, int qos = 0, bool retain = false);
    bool MqttConnectStatus();

    void message_callback_add(const std::string &topic, onMessageCallback callback);

private:
    std::string address_;
    short port;

    std::atomic_bool _connect_status;

    std::string _robot_sn;
    onMessageCallback _message_callback;

    int64_t _last_message_recv_time;    // 最近一次消息接受的时间 ms
    std::unordered_map<std::string, onMessageCallback> _msg_callback_map;
};

}
#endif // !__MQTT_CLIENT_H__

